define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'products/index' + location.search,
                    add_url: 'products/add',
                    edit_url: 'products/edit',
                    del_url: 'products/del',
                    multi_url: 'products/multi',
                    import_url: 'products/import',
                    table: 'products',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'product_name', title: __('Product_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'type', title: __('type'),searchList: {"0":'儿童',"1":'老年'}, formatter: Table.api.formatter.label},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'tag', title:'分类', operate:'BETWEEN'},
                        {field: 'remarks', title: __('remarks') ,operate: 'none',
                            formatter: function(value) {
                                return `<span title="${value}">${value.length > 20 ? value.substring(0, 20) + '...' : value}</span>`; // 只显示前20个字符
                            } },
                        {field: 'recommend', title: "首页推荐",
                            searchList: {"0":"关闭","1":"开启"},
                            formatter: Table.api.formatter.toggle,
                            search:false
                        },

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function(value, row, index) {
                                return [
                                    '<a class="btn btn-xs btn-success btn-editone" ',
                                    'href="/houtai.php/products/edit/ids/' + row.id + '?dialog=1" title="编辑">',
                                    '<i class="fa fa-pencil"></i> 编辑</a> ',

                                    '<a class="btn btn-xs btn-danger btn-delone" ', // 添加删除按钮
                                    'href="javascript:void(0);" title="删除" data-id="' + row.id + '" data-toggle="modal" data-target="#deleteModal">',
                                    '<i class="fa fa-trash"></i> 删除</a>'
                                ].join('');
                            }}

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $(document).ready(function() {
                $('.radio input[name="row[type]"]').on('click', function() {
                    var type = $(this).val(); // 获取选中的
                    // 清空 availability-select 的选项
                    $('#tagid-select').empty().append('<option value="">加载中...</option>').selectpicker('refresh');
                    append(type);
                });
                append(0);
            });

            function append(type){

                // 发起 AJAX 请求获取可用性选项
                $.ajax({
                    url: 'worktags/tags', // 替换为实际的 API 地址
                    type: 'GET',
                    data: { type: type },
                    success: function(data) {
                        // 假设返回的数据格式是 { success: true, options: [...] }
                        if (data.code==1) {
                            var options = data.list;
                            // 清空之前的选项
                            $('#tagid-select').empty();
                            // 添加新的选项
                            options.forEach(function(option) {
                                $('#tagid-select').append($('<option>', {
                                    value: option.id,
                                    text: option.name// 假设返回的数据有 id 和 name
                                }));
                            });
                        } else {
                            $('#tagid-select').empty().append('<option value="">没有可用的选项</option>');
                        }
                        $('#tagid-select').selectpicker('refresh'); // 更新 selectpicker 显示

                    },
                    error: function() {
                        $('#tagid-select').empty().append('<option value="">加载失败</option>');
                        $('#tagid-select').selectpicker('refresh');
                    }
                });
            }
        },
        edit: function () {
            Controller.api.bindevent();
            $(document).ready(function() {
                $('.radio input[name="row[type]"]').on('click', function() {
                    var type = $(this).val(); //
                    // 清空 availability-select 的选项
                    $('#tagid-select').empty().append('<option value="">加载中...</option>').selectpicker('refresh');
                    append(type);
                });
            });

            function append(type){

                // 发起 AJAX 请求获取可用性选项
                $.ajax({
                    url: 'worktags/tags', // 替换为实际的 API 地址
                    type: 'GET',
                    data: { type: type },
                    success: function(data) {
                        // 假设返回的数据格式是 { success: true, options: [...] }
                        if (data.code==1) {
                            var options = data.list;
                            // 清空之前的选项
                            $('#tagid-select').empty();
                            // 添加新的选项
                            options.forEach(function(option) {
                                $('#tagid-select').append($('<option>', {
                                    value: option.id,
                                    text: option.name// 假设返回的数据有 id 和 name
                                }));
                            });
                        } else {
                            $('#tagid-select').empty().append('<option value="">没有可用的选项</option>');
                        }
                        $('#tagid-select').selectpicker('refresh'); // 更新 selectpicker 显示

                    },
                    error: function() {
                        $('#tagid-select').empty().append('<option value="">加载失败</option>');
                        $('#tagid-select').selectpicker('refresh');
                    }
                });
            }
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
